package com.yxym.encap;

import com.yxym.modifier.A;

public class Encaplation01 {
    /*
    设置个人的信息，不能查看年龄，工资等，并设置年龄进行合理的验证，年龄必须在
    18-100，年龄和工资不能被直接查看，name的长度在2-6之间
     */
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("yxy");
        person.setAge(3020);
        person.setSalary(30000.0);
        System.out.println(person.info());

        //使用自己的构造器指定属性
        Person person2 =  new Person();
        person2.setSalary(20000.0);
        person2.setName("dd");
        person2.setAge(25);
        System.out.println(person2.info());
    }
}
class Person{
    public String name;
    private int age;
    private double salary;
    public Person(){
    }
    //三个属性构造器
    public Person(String name,int age,double salary) {
//        this.name = name;
//        this.age = age;
//        this.salary = salary;
        //将set方法写入构造器，可以认证写入的数据
        setSalary(salary);
        setAge(age);
        setName(name);
    }
    //这里可以使用快捷键alt+insert再选Getters and Setters全部选中回车
    public String getName() {
        return name;
    }

    public void setName(String name) {
        if(name.length() >= 2 && name.length() <= 6 ){
            this.name = name;
        }else{
            System.out.println("输入的名字长度不对，重新输入（范围为2-6）");
            this.name = "无名氏";
        }
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(age <= 100 && age >= 18 ){
            this.age = age;
        }else{
            System.out.println("输入的年龄数据有误，重新输入");
            this.age = 18;
        }
    }

    public double getSalary() {
        //增加权限管理
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
    //返回属性
    public String info(){
        return "输出信息为：" + "name=" + name+ "\t" + "age=" + age
                + "\t"+ "salary=" + salary;
    }
}
